/**
 * Created by zhaoxk on 2017/2/22.
 */
import React from 'react';
import _ from 'lodash';
import styles from './index.less';

function ListCustom({ data, items, style = {}, border }) {
    if (data && data.length > 0) {
        const borderStyle = border !== false ? { borderBottom: '1px solid #ccc' } : {};
        const renderItems = () => _.map(data, (d, i) => {
            const key = d.id || d.code;
            if (!key) {
                throw new Error('传入ListCustom组件的数据集合中缺少“id”或者“code”类型的字段，单个数据无法确保唯一性');
            }
            const last = data.length - 1;
            return (
                <li
                    key={key}
                    style={i !== last ? borderStyle : {}}
                    className={styles.item}
                >
                    {items(d)}
                </li>
            );
        });
        return (
            <ul
                style={style}
                className={styles.normal}
            >
                {renderItems()}
            </ul>
        );
    }
    return (
        <div>暂无数据</div>
    );
}

export default ListCustom;
